﻿Imports System.Drawing

Public Class mainGame

    Dim resWidth As Integer = 1010
    Dim resHeight As Integer = 550
    Public tileSize As Integer = 32

    Dim G As Graphics
    Dim BBG As Graphics
    Dim BB As Bitmap
    Dim bmpTile As Bitmap
    Dim sRect As Rectangle
    Dim dRect As Rectangle

    ' Stats
    Dim MoveSpeed As Integer = 8 '!!!!!per key stroke movement 12 8 4 
    Dim lvl As Integer = 1

    'keys made better
    Dim keyPushed As Short = 0
    Dim ponyDirection As Integer

    Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Integer) As Short

    'Dungeon Map
    Private dungeonMap As DungeonMap
    Dim MapX As Integer
    Dim MapY As Integer

    'gut position checker
    Dim guyX As Decimal = (MapX + 9) * tileSize
    Dim guyY As Decimal = (MapX + 6) * tileSize


    Dim IsRunning As Boolean = True

    ' make new font
    Dim mainFont As Font = New Font("Verdana", 12)

    'mouse stuffs 
    Dim mouseX As Integer
    Dim mouseY As Integer
    Dim mMapX As Integer
    Dim mMapY As Integer

    'toon var's
    Dim xPos As Integer = 0
    Dim yPos As Integer = 0
    Dim MoveDirection As Short = 0
    Dim LastDirection As Short = 2

    'Textures
    Dim bmpToon As Bitmap
    Dim bmpLogo As Bitmap
    Dim bmpExit As Bitmap
    Dim bmpClear As Bitmap
    Dim bmpAbout As Bitmap
    Dim bmpPony As Bitmap

    'paint brush
    Dim paintBrush As Integer = 0

    'Apple stuffs
    Dim numMoves As Integer = 0
    Dim random As Random

    'Creatures
    Dim creatures(0) As Creature

    Private Sub StartGame(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Size = New Size(1000, 500)
        Me.Show()
        Me.Focus()

        G = Me.CreateGraphics
        BB = New Bitmap(resWidth, resHeight) ' size
        bmpTile = New Bitmap(My.Resources.textures)

        'initialize map
        dungeonMap = New DungeonMap(50)

        'Puts Character in lower left room
        MapX = -5
        MapY = dungeonMap.getSizeY() - 15

        'Defines Textures
        bmpToon = New Bitmap(My.Resources.Characterv3)
        bmpLogo = New Bitmap(My.Resources.treasureSeekerLogo)
        bmpExit = New Bitmap(My.Resources.ExitButton)
        bmpClear = New Bitmap(My.Resources.ClearButton)
        bmpAbout = New Bitmap(My.Resources.AboutButton)
        bmpPony = New Bitmap(My.Resources.RedPony)

        generateCreatures()

        startgameloop()
    End Sub

    Private Function GetKeyState(ByVal key1 As Integer) As Boolean
        Dim s As Short
        s = GetAsyncKeyState(key1)
        If s = 0 Then Return False

        Return True
    End Function

    Private Sub exitGame() Handles Me.FormClosed
        IsRunning = False
        showResults() 'Uncomment when not debuging
    End Sub

    Private Sub SetMoveDir()
        If GetKeyState(Keys.W) = True Then MoveDirection = 1
        If GetKeyState(Keys.S) = True Then MoveDirection = 2
        If GetKeyState(Keys.A) = True Then MoveDirection = 3
        If GetKeyState(Keys.D) = True Then MoveDirection = 4

        'Pony Movement
        'If GetKeyState(Keys.I) = True Then ponyDirection = 0
        'If GetKeyState(Keys.K) = True Then ponyDirection = 1
        'If GetKeyState(Keys.J) = True Then ponyDirection = 2
        'If GetKeyState(Keys.L) = True Then ponyDirection = 3

        'If GetKeyState(Keys.I) = False And _
        '    GetKeyState(Keys.K) = False And _
        '    GetKeyState(Keys.J) = False And _
        '    GetKeyState(Keys.L) = False Then ponyDirection = 4
        '/Pony Movement

        'Stop complaining Huggins!
        If GetKeyState(Keys.Up) = True Then MoveDirection = 1
        If GetKeyState(Keys.Down) = True Then MoveDirection = 2
        If GetKeyState(Keys.Left) = True Then MoveDirection = 3
        If GetKeyState(Keys.Right) = True Then MoveDirection = 4

        If GetKeyState(Keys.Up) = False And _
            GetKeyState(Keys.Down) = False And _
            GetKeyState(Keys.Left) = False And _
            GetKeyState(Keys.Right) = False And _
            GetKeyState(Keys.W) = False And _
            GetKeyState(Keys.S) = False And _
            GetKeyState(Keys.A) = False And _
            GetKeyState(Keys.D) = False Then
            MoveDirection = 0
        End If

        If MoveDirection <> 0 Then LastDirection = MoveDirection
    End Sub

    Private Sub GetToon(ByVal dir As Short) '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        Select Case dir 'need these 4 sprites using one color
            Case 1
                'up
                sRect = New Rectangle(0, 0, 46, 46)
            Case 2
                'down
                sRect = New Rectangle(48, 0, 41, 46)
            Case 3
                'left
                sRect = New Rectangle(89, 0, 43, 46)
            Case 4
                'right
                sRect = New Rectangle(132, 0, 43, 46)
        End Select
    End Sub

    Private Sub startgameloop()
        Do While IsRunning = True
            'keeps app responsive
            Application.DoEvents()

            SetMoveDir()
            MoveToon(MoveDirection)

            For Each c As Creature In creatures
                If ponyDirection <> 4 Then
                    c.move(dungeonMap, ponyDirection)
                Else
                    c.moveRandom(dungeonMap)
                End If
            Next
            ponyDirection = 4

            DrawGraphics()

            checkWinCondition()
        Loop
    End Sub

    Private Sub DrawGraphics()
        'Draws map
        For x = -1 To 15 ' SET TO FIRST 0 FOR PROOF OF MOVEMENT
            For y = -1 To 15 ' SET TO FIRST 0 FOR PROOF OF MOVEMENT
                sRect = getSourceRect(MapX + x, MapY + y)

                dRect = New Rectangle((x * tileSize) + xPos, (y * tileSize) + yPos, tileSize, tileSize)
                G.DrawImage(bmpTile, dRect, sRect, GraphicsUnit.Pixel)

            Next
        Next
        'Draws Creatures
        For x = -1 To 15
            For y = -1 To 15
                Dim drawCreature As Creature = Array.Find(creatures, Function(cre) cre.getXCoord = MapX + x And cre.getYCoord = MapY + y)
                If drawCreature IsNot Nothing Then
                    G.DrawImage(drawCreature.getTexture, New Point((x * tileSize) + xPos + drawCreature.getXOffset, (y * tileSize) + yPos + drawCreature.getYOffset))
                End If
            Next
        Next

        'Buttons
        G.FillRectangle(Brushes.Black, 0, 0, 992, tileSize)  'draw upper boarder 
        G.FillRectangle(Brushes.Black, 0, 423, 1000, tileSize * 2) 'draw lower boarder
        G.FillRectangle(Brushes.Black, 0, 0, tileSize, 423)  'draw left boarder
        G.FillRectangle(Brushes.Black, 960, 0, tileSize, 423) 'draw right boarder
        G.FillRectangle(Brushes.Black, 480, 0, 56, 423) 'draw middle divider
        G.DrawImage(bmpLogo, 536, 16, 424, 100) 'draw logo area

        'draw buttons
        G.DrawImage(bmpClear, 645, 275, 200, 50)
        G.DrawImage(bmpExit, 645, 350, 200, 50)
        G.DrawImage(bmpAbout, 920, 383)

        'draw toon
        GetToon(LastDirection)
        bmpToon.MakeTransparent(Color.White) 'ALPHA
        G.DrawImage(bmpToon, 9 * tileSize, 6 * tileSize, sRect, GraphicsUnit.Pixel)

        G.DrawString("Score: " & numMoves & vbCrLf & _
                     "Speed: " & MoveSpeed & vbCrLf & _
                     "Level: " & lvl & vbCrLf & _
                     "", mainFont, Brushes.Black, 690, 150)

        'DEV STRINGS
        'G.DrawString("DEBUG:    (" & MapX & ", " & MapY & ")", mainFont, Brushes.Black, 690, 230)

        G = Graphics.FromImage(BB)

        BBG = Me.CreateGraphics
        BBG.DrawImage(BB, 0, 0, resWidth, resHeight)

        G.Clear(Color.Wheat)
    End Sub

    Private Sub MoveToon(ByVal dir As Short)
        Select Case dir
            Case 1
                If dungeonMap.collides(MapX + 9, MapY + 5) Then
                Else
                    yPos += MoveSpeed

                    If yPos >= tileSize Then
                        yPos = 0
                        MapY -= 1
                        numMoves += 1
                    End If
                End If

            Case 2
                If dungeonMap.collides(MapX + 9, MapY + 7) Then
                Else
                    yPos -= MoveSpeed
                    If yPos <= tileSize * -1 Then
                        yPos = 0
                        MapY += 1
                        numMoves += 1
                    End If
                End If
            Case 3
                If dungeonMap.collides(MapX + 8, MapY + 6) Then
                Else
                    xPos += MoveSpeed
                    If xPos >= tileSize Then
                        xPos = 0
                        MapX -= 1
                        numMoves += 1
                    End If
                End If

            Case 4
                If dungeonMap.collides(MapX + 10, MapY + 6) Then
                Else
                    xPos -= MoveSpeed
                    If xPos <= tileSize * -1 Then
                        xPos = 0
                        MapX += 1
                        numMoves += 1
                    End If
                End If
        End Select
    End Sub

    Private Sub Form1_MouseClick(sender As Object, e As MouseEventArgs) Handles Me.MouseClick
        ' MsgBox(" you attacked " & mMapX & " : " & mMapY)

        'Exit
        If e.X > 645 And e.X < 845 And e.Y > 350 And e.Y < 400 Then
            IsRunning = False
            close()

            'Clear
        ElseIf e.X > 645 And e.X < 845 And e.Y > 275 And e.Y < 325 Then
            showResults()
            lvl = 1
            numMoves = 0
            MoveSpeed = 8
            dungeonMap = New DungeonMap(50)
            generateCreatures()

            MapX = -5
            MapY = dungeonMap.getSizeY() - 15

        ElseIf e.X > 920 And e.X < 960 And e.Y > 383 And e.Y < 423 Then
            'MsgBox("You pressed the clear area!", MsgBoxStyle.OkOnly, "Clear!")
            Credits_Info.Show()
        End If
    End Sub

    Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
        mouseX = Math.Floor(e.X)
        mouseY = Math.Floor(e.Y)
        mMapX = MapX + mouseX
        mMapY = MapY + mouseY

    End Sub

    Private Function getSourceRect(ByVal xCoord As Integer, ByVal yCoord As Integer) As Rectangle
        Select Case dungeonMap.getTile(xCoord, yCoord)
            Case 0 'Void
                Return New Rectangle(0, 0, tileSize, tileSize)
            Case 1 'Wall
                Return New Rectangle(32, 0, tileSize, tileSize)
            Case 2 'Floor
                Return New Rectangle(64, 0, tileSize, tileSize)
            Case 3 'Water
                Return New Rectangle(96, 0, tileSize, tileSize)
        End Select
    End Function

    Private Function getNext() As Boolean
        Return True
    End Function

    Private Sub checkWinCondition()
        If dungeonMap.getTile(MapX + 9, MapY + 6) = 3 Or _
           dungeonMap.getTile(MapX + 10, MapY + 6) = 3 Or _
           dungeonMap.getTile(MapX + 9, MapY + 7) = 3 Or _
           dungeonMap.getTile(MapX + 10, MapY + 7) = 3 Then

            lvl += 1
            MoveSpeed += 5

            random = New Random()
            Dim newMapX = random.Next(dungeonMap.getSizeX + (dungeonMap.getSizeX / 4), dungeonMap.getSizeX * 2)
            Dim newMapy = random.Next(dungeonMap.getSizeY + (dungeonMap.getSizeY / 4), dungeonMap.getSizeY * 2)
            dungeonMap = New DungeonMap(newMapX, newMapy)

            MapX = -5
            MapY = dungeonMap.getSizeY() - 15
        End If

        For i As Integer = 0 To creatures.Length - 1
            Dim cre As Creature = creatures(i)
            If cre.getXCoord = MapX + 9 And cre.getYCoord = MapY + 6 Or _
                cre.getXCoord = MapX + 9 And cre.getYCoord = MapY + 7 Or _
                cre.getXCoord = MapX + 9 And cre.getYCoord = MapY + 6 Or _
                cre.getXCoord = MapX + 9 And cre.getYCoord = MapY + 7 Then
                creatures(i) = New Creature(-10, -10, bmpPony)
                numMoves -= 20
            End If
        Next

    End Sub

    Private Sub showResults()
        MsgBox("You have moved " & numMoves & " times and found " & lvl - 1 & " treasure chests! Congratulations!", MsgBoxStyle.OkOnly, "Game Over!")
    End Sub

    Private Sub generateCreatures()
        'Generate Creatures
        ReDim creatures(0)
        Dim xCoord As Integer
        Dim yCoord As Integer
        random = New Random()
        For i As Integer = 0 To 100
            If random.Next(100) < 25 Then
                'Makes the array one size larger
                ReDim Preserve creatures(UBound(creatures) + 1)
                Dim boolInvalidPosition = True
                While boolInvalidPosition
                    xCoord = random.Next(dungeonMap.getSizeX)
                    yCoord = random.Next(dungeonMap.getSizeY)

                    If dungeonMap.isInsideDungeon(xCoord, yCoord) Then
                        boolInvalidPosition = False
                    End If
                End While

                creatures(creatures.Length - 1) = New Creature(xCoord, yCoord, bmpPony)
            End If
        Next

        'Dev Pony. Places it near spawn to see if code works.
        creatures(0) = New Creature(5, 45, bmpPony)

    End Sub

End Class
